/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.report.html;

import java.io.IOException;
import java.text.DecimalFormat;

import org.jacoco.report.internal.html.HTMLElement;

/**
 * Tool Echarts for coverage reports in multiple HTML pages.
 */
public class ToolEcharts {

	public static void drawGraph(HTMLElement divItem, HTMLElement graphBody,
			String index, String title, int cover, int miss, double percent) {
		String percentStr = null;
		if (Double.isNaN(percent)) {
			percentStr = "0.00";
		} else {
			percent = percent * 100;
			// 设置转换格式
			DecimalFormat df = new DecimalFormat("0.00");
			// 转换格式
			percentStr = df.format(percent);
		}
		StringBuilder strB = new StringBuilder();
		strB.append("\n");
		strB.append("	var chartDom = document.getElementById('container"
				+ index + "');	 \n");
		strB.append("	var myChart = echarts.init(chartDom);	 \n");
		strB.append("	var option = {	 \n");
		strB.append("		  tooltip: {	 \n");
		strB.append("			trigger: 'item'	 \n");
		strB.append("		  },	 \n");
		strB.append("		  legend: {	 \n");
		strB.append("			top: '2%',	 \n");
		strB.append("			left: 'center'	 \n");
		strB.append("		  },	 \n");
		strB.append(
				"		  color: ['rgb(227 97 56)', '#64dd59'],// 饼图各块颜色	 \n");
		strB.append("		  series: [	 \n");
		strB.append("			{	 \n");
		strB.append("			  name: '',	 \n");
		strB.append("			  type: 'pie',	 \n");
		strB.append("			  radius: ['80%', '90%'],	 \n");
		strB.append("			  avoidLabelOverlap: false,	 \n");
		strB.append("			  label: {	 \n");
		strB.append("				position: 'center',	 \n");
		strB.append("				show: true,	 \n");
		strB.append("				formatter:() => {	 \n");
		strB.append("					let str = '" + title + percentStr
				+ "%'	 \n");
		strB.append("					return str	 \n");
		strB.append("				},	 \n");
		strB.append("				color: '#1E7CE8',	 \n");
		strB.append("				lineHeight: 16,	 \n");
		strB.append("				fontSize: 12,	 \n");
		strB.append("			},	 \n");
		strB.append("			  emphasis: {	 \n");
		strB.append("				label: {	 \n");
		strB.append("				  show: false,	 \n");
		strB.append("				  fontSize: 12,	 \n");
		strB.append("				  fontWeight: 'bold'	 \n");
		strB.append("				}	 \n");
		strB.append("			  },	 \n");
		strB.append("			  labelLine: {	 \n");
		strB.append("				show: false	 \n");
		strB.append("			  },	 \n");
		strB.append("			  data: [	 \n");
		strB.append("				{ value: " + miss + "},	 \n");
		strB.append("				{ value: " + cover + "}	 \n");
		strB.append("			  ]	 \n");
		strB.append("			}	 \n");
		strB.append("		  ]	 \n");
		strB.append("		};	 \n");
		strB.append("	option && myChart.setOption(option);	 \n");

		try {
			divItem.attr("style", "height: 160px;width:160px;float:left");
			divItem.attr("id", "container" + index);
			divItem.scriptContent(strB.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void drawGraphHighcharts(HTMLElement divItem, HTMLElement graphBody,
			String index, String title, int cover, int miss, double percent) {
		if (Double.isNaN(percent))
			percent = 0.0000;
		StringBuilder strB = new StringBuilder();
		strB.append("\n");
		strB.append("	var chart = Highcharts.chart('container" + index
				+ "', {	 \n");
		strB.append("		chart: {                                 \n");
		strB.append("			spacing : [10, 0 , 10, 0]            \n");
		strB.append("		},                                       \n");
		strB.append("		title: {                                 \n");
		strB.append("			floating:true,                       \n");
		strB.append(
				"			text: ' " + title + "圆心显示的标题'               \n");
		strB.append("		},                                       \n");
		strB.append("		tooltip: {                               \n");
		strB.append(
				"			pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'     \n");
		strB.append(
				"		},                                                                   \n");
		strB.append(
				"		plotOptions: {                                                       \n");
		strB.append(
				"			pie: {                                                           \n");
		strB.append(
				"				allowPointSelect: false,                                     \n");
		strB.append(
				"				cursor: 'pointer',                                           \n");
		strB.append(
				"				dataLabels: {                                                \n");
		strB.append(
				"					enabled: true,                                           \n");
		strB.append(
				"					format: '<b>{point.name}</b>: {point.percentage:.1f} %', \n");
		strB.append(
				"					style: {                                                 \n");
		strB.append(
				"						color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'   \n");
		strB.append(
				"					}                                                                 \n");
		strB.append(
				"				}                                                      \n");
		strB.append(
				"			}                                                          \n");
		strB.append(
				"		},                                                             \n");
		strB.append(
				"		series: [{                                                     \n");
		strB.append(
				"			type: 'pie',                                               \n");
		strB.append(
				"			innerSize: '80%',                                          \n");
		strB.append(
				"			name: '市场份额',                                                                 \n");
		strB.append(
				"			data: [                                                                 \n");
		strB.append(
				"				['Opera',     6.2],                                                                 \n");
		strB.append(
				"				['其他',   0.7]                                         \n");
		strB.append(
				"			]                                                           \n");
		strB.append(
				"		}]                                                              \n");
		strB.append(
				"	}, function(c) {                                                    \n");
		strB.append(
				"		var centerY = c.series[0].center[1],                            \n");
		strB.append(
				"			titleHeight = parseInt(c.title.styles.fontSize);            \n");
		strB.append(
				"		c.setTitle({                                                    \n");
		strB.append(
				"			y:centerY + titleHeight/2                                   \n");
		strB.append(
				"		});                                                             \n");
		strB.append(
				"	});                                                                 \n");
		try {
			divItem.attr("style", "height: 160px;width:160px;float:left");
			divItem.attr("id", "container" + index);
			divItem.scriptContent(strB.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param 覆盖数
	 * @param 未覆盖数
	 * @return '0.0%', '0.0%'
	 */
	public static String calculatePercentage(int cover, int miss) {
		int divisor = cover + miss;
		if (divisor == 0) {
			return "'0.0%', '0.0%'";
		}
		// 计算商
		double quotient = cover / divisor;
		// 设置转换格式
		DecimalFormat df = new DecimalFormat("0.00");
		// 转换格式
		String result = df.format(quotient);
		double missPercent = 100 - Double.parseDouble(result);
		return "'" + result + "%', '" + missPercent + "%'";
	}
}
